package cn.eeepay.boss.action.aatAgentActivity;

import cn.eeepay.boss.system.DataSource;
import cn.eeepay.boss.system.SystemLog;
import cn.eeepay.framework.db.pagination.Page;
import cn.eeepay.framework.model.aatAgentActivity.AppMenu;
import cn.eeepay.framework.service.aatAgentActivity.AppMenuActionService;
import cn.eeepay.framework.util.Constants;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "/appMenuAction")
public class AppMenuAction {
    private static final Logger log = LoggerFactory.getLogger(AppMenuAction.class);

    @Resource
    private AppMenuActionService appMenuActionService;

    /**
     * 查询列表
     */
    @RequestMapping(value = "/selectAll")
    @ResponseBody
    @DataSource(Constants.DATA_SOURCE_SLAVE)
    public Map<String,Object> selectByParam(@RequestParam("info") String param, @ModelAttribute("page") Page<AppMenu> page){
        Map<String, Object> msg=new HashMap<String,Object>();
        try{
            AppMenu info = JSONObject.parseObject(param, AppMenu.class);
            appMenuActionService.selectAllList(info, page);
            msg.put("page",page);
            msg.put("status", true);
        } catch (Exception e){
            log.error("查询列表异常!",e);
            msg.put("status", false);
            msg.put("msg", "查询列表异常!");
        }
        return msg;
    }
    /**
     * 保存APP菜单
     */
    @RequestMapping(value = "/saveInfo")
    @ResponseBody
    @SystemLog(description = "保存APP菜单",operCode="appMenuAction.saveInfo")
    public Map<String,Object> saveInfo(@RequestParam("param") String param){
        Map<String, Object> msg=new HashMap<String,Object>();
        try{
            AppMenu info = JSONObject.parseObject(param, AppMenu.class);
            int num=appMenuActionService.saveInfo(info);
            if(num>0){
                msg.put("msg","保存成功");
                msg.put("status", true);
            }else{
                msg.put("msg","保存失败");
                msg.put("status", false);
            }
        } catch (Exception e){
            log.error("保存APP菜单异常!",e);
            msg.put("status", false);
            msg.put("msg", "保存APP菜单异常!");
        }
        return msg;
    }
    /**
     * 操作开关
     */
    @RequestMapping(value = "/saveSwitch")
    @ResponseBody
    @SystemLog(description = "APP菜单开关",operCode="appMenuAction.saveSwitch")
    public Map<String,Object> saveSwitch(@RequestParam("id") int id,@RequestParam("sta") String sta){
        Map<String, Object> msg=new HashMap<String,Object>();
        try{
            int num=appMenuActionService.saveSwitch(id,sta);
            if(num>0){
                msg.put("msg","操作成功");
                msg.put("status", true);
            }else{
                msg.put("msg","操作失败");
                msg.put("status", false);
            }
        } catch (Exception e){
            log.error("操作开关异常!",e);
            msg.put("status", false);
            msg.put("msg", "操作开关异常!");
        }
        return msg;
    }
}
